/*!
 Temelia - Hash map performance tests.

 Copyright (C) 2008 Ceata (http://cod.ceata.org/proiecte/temelia).

 @author Dascalu Laurentiu

 This program is free software; you can redistribute it and
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 3
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "include/performance.hpp"
#include "include/testutil.hpp"

extern "C"
{
#include <temelia/hash_map.h>
}

static hash_map_t __temelia_hash_map_put(int n)
{
	hash_map_t map;

	map = hash_map_new();

	/*
	 * If you don't want to compare the keys, because it may be impossible or
	 * too expensive then give as comparison function this:
	 *
	 * int my_compare(void *key1, void *key2)
	 * {
	 *     if (key1 == key2)
	 *         return 0;
	 *     else if (key1 < key2)
	 *         return -1;
	 *     return 1;
	 * }
	 */
	for (int i = 0; i < n; i++)
		hash_map_put(map, default_collection[i], default_collection[i],
				int_compare, NULL);

	return map;
}

static void temelia_hash_map_put(int n)
{
	hash_map_delete(__temelia_hash_map_put(n));
}

static void temelia_hash_map_contains_key(int n)
{
	hash_map_t map;

	map = __temelia_hash_map_put(n);

	for (int i = 0; i < n; i++)
		hash_map_contains_key(map, default_collection[i], int_compare, NULL);

	hash_map_delete(map);
}

static void temelia_hash_map_remove_key(int n)
{
	hash_map_t map;

	map = __temelia_hash_map_put(n);

	for (int i = 0; i < n; i++)
		hash_map_remove_key(map, default_collection[i], int_compare, NULL);

	hash_map_delete(map);
}

static void __null_handler(void *key, void *value, void *context)
{

}

static void temelia_hash_map_iterate(int n)
{
	hash_map_t map;

	map = __temelia_hash_map_put(n);
	hash_map_iterate(map, __null_handler, NULL);

	hash_map_delete(map);
}

void TemeliaPerformance::pt_hash_map(void)
{
	TestUnit testUnit("hash_map");

	GENERATE_TEMELIA_TEST(testUnit, "hash_map_put", hash_map_put);
	GENERATE_TEMELIA_TEST(testUnit, "hash_map_contains_key", hash_map_contains_key);
	GENERATE_TEMELIA_TEST(testUnit, "hash_map_remove_key", hash_map_remove_key);
	GENERATE_TEMELIA_TEST(testUnit, "hash_map_iterate", hash_map_iterate);

	testUnit.write(fout);
}
